<?php

class Video
{	
	public $db;
	public $table;
	public $t_matchs;
	
	public function __construct()
	{
	    $this->db=new Mysql();
	    $this->table=$this->db->pre.'videos';
	    $this->t_matchs=$this->db->pre.'matchs';
	    $this->t_stars=$this->db->pre.'stars';
	
	}
	
	public function getAllVideos(){
	    $sql = "SELECT * FROM ".$this->table." WHERE 1 ";
	    if (Http::get('match')){
	        $sql = $sql." AND `match` LIKE '%".Http::get('match')."%' ";
	    }
	    if (Http::get('star')){
	        $sql = $sql." AND `star` LIKE '%".Http::get('star')."%' ";
	    }
	    
	    if (Http::get('title')){
	        $sql = $sql." AND `title` LIKE '%".Http::get('title')."%' ";
	    }
	    if (!Http::get('stimeline')){
	        Http::setGet('stimeline',date("Y-m-d",strtotime('-10 days')));	        
	    }
	    $sql = $sql." AND createTime > '".Http::get('stimeline')." 00:00:00' ";
	    if (!Http::get('etimeline')){
	        Http::setGet('etimeline',date("Y-m-d"));	        
	    }
	    $sql = $sql." AND createTime <= '".Http::get('etimeline')." 23:59:59' ";
	    
	    $page = new Page ();	  
	     
	    if (Http::get ( 'orderby' )) {
	        $page->orderBy = Http::get ( 'orderby' );
	        $page->desc = Http::get ( 'sc' );
	    } else {
	        $page->orderBy = 'id';
	    }
	    $page->uriStyle = '?';
	    $list = $page->getPage ( $sql, 0 );
	    $html = $page->htmlPage ();
	    //返回搜索结果和分页信息
	    return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
	}
	public function getOneVideo($id)
	{
	    $sql = "SELECT * FROM ".$this->table." WHERE id = $id ";
	    return $this->db->getRow($sql);
	}
	
	public function getAllMatchs($page=true){
	    $sql = "SELECT * FROM ".$this->t_matchs." WHERE 1 ";
	    if ($page){
    	    if (Http::get('name')){
    	        $sql = $sql." AND `name` LIKE '%".Http::get('name')."%' ";
    	    }
    	    if (Http::get('type')){
    	        $sql = $sql." AND `type` LIKE '%".Http::get('type')."%' ";
    	    }
    	    if (Http::get('space')){
    	        $sql = $sql." AND `space` LIKE '%".Http::get('space')."%' ";
    	    }
    	    
    	    if (Http::get('stimeline')){
    	        #Http::setGet('stimeline',date("Y-m-d",strtotime('-10 days')));
    	        $sql = $sql." AND stime > '".Http::get('stimeline')." 00:00:00' ";
    	    }
    	    
    	    if (Http::get('etimeline')){
    	        #Http::setGet('etimeline',date("Y-m-d"));
    	        $sql = $sql." AND stime <= '".Http::get('etimeline')." 23:59:59' ";
    	    }
    	    
	    
	        $page = new Page ();	
    	    if (Http::get ( 'orderby' )) {
    	        $page->orderBy = Http::get ( 'orderby' );
    	        $page->desc = Http::get ( 'sc' );
    	    } else {
    	        $page->orderBy = 'id';
    	    }
    	    $page->uriStyle = '?';
    	    $list = $page->getPage ( $sql, 0 );
    	    $html = $page->htmlPage ();
    	    //返回搜索结果和分页信息
    	    return array ('list' => $list, 'html' => $html, 'total' => $page->allNums, 'eachNums' => $page->eachNums );
	    }else{
	        return $this->db->getAll($sql);
	    }
	}
	public function getOneMatchs($id)
	{
	    $sql = "SELECT * FROM ".$this->t_matchs." WHERE id = $id ";
	    return $this->db->getRow($sql);
	}
	
	public function getAllStars(){
	    
	   
	    $sql = "SELECT star,tostar FROM ".$this->table." GROUP BY star,tostar ";
	    $all = $this->db->getAll($sql);
	    $star=array();
	    $i=0;
	    foreach ($all as $k=>$v){	         
            $star[$i]=$v['star'];
            $i++;
            $star[$i]=$v['tostar'];
            $i++;	        
	    }
	    $returnStar = array();
	    foreach ($star as $k=>$v){	       
	        if (in_array($v, $returnStar)){	            
	            continue;
	        }else {
	            $returnStar[]=$v;
	        }
	    }
	    
	    return $returnStar;
	}
	public function getOneStars($id)
	{
	    $sql = "SELECT * FROM ".$this->t_stars." WHERE id = $id ";
	    return $this->db->getRow($sql);
	}
}